﻿using System;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class NeedPlanWindow : EFViewCmes
    {
        IServiceAgent service;
       public event EventHandler<EventArgs> UpdatePlan;
        public NeedPlanWindow(String _dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(_dialogType);
            service = new HttpServiceAgent();
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.needplanWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }
        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }

        #region 确定/返回按钮事件
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Commit_Click(object sender, RoutedEventArgs e)
        {
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;

            int count = this.grid.GetCheckedEIRows().Count;
            if (count < 1)
            {
                EIInfo eiInfo = new EIInfo();
                EIBlock _block = new EIBlock("result");
                eiInfo.AddBlock(_block);
                eiInfo.Set("service", "ZMCMD3");
                eiInfo.Set("method", "insert");
                eiInfo.Set("fdParentId", this.fdParentId.Value);
                EIBlock eiBlock = this.grid.EIBlock == null ? new EIBlock("result") : this.grid.EIBlock;
                eiInfo.AddBlock(eiBlock);
                base.Submit(eiInfo, DoInsertCompleted);
            }
            else
            {
                EIInfo eiInfo = new EIInfo();
                EIRow _row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
                if (null != this.grid.CheckedEIRows)
                {
                    base.Submit(OperationMode.Insert, "ZMCMD3", "insert", DataRange.CheckedItems, DoInsertOperationCompleted, null);
                }
            }      
        }

        /// <summary>
        /// 新增成功后回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoInsertCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eIInfo = e.EIInfo;
            if (eIInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                base.OnCommitMessagePopup(eIInfo);//操作失败，消息提醒
            }
            else
            {
                EFWindow window = this.Parent as EFWindow;          //操作成功关闭新增页面
                window.DialogResult = true;                         //此处可以扩展是否连续新增
                window.Close();
            }
        }
      /// <summary>
        /// 新增成功后回调
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void DoInsertOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eIInfo = e.EIInfo;
            if (eIInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                base.OnCommitMessagePopup(eIInfo);//操作失败，消息提醒
            }
            else
            {
                if (UpdatePlan != null)
                {
                    UpdatePlan.Invoke(this, null);
                }
                e.EIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
                ChangeNeedforYearPlanWindow changeForYaer = new ChangeNeedforYearPlanWindow("insert");//构造一个新的修改页面
                changeForYaer.EIDataContext = new Platform.Core.UI.EIDataContext();
                changeForYaer.EIDataContext.Update(e.EIInfo);
                CmesWindows _popup = this.Parent as CmesWindows;//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = "按需求计划调整";
                _popup.Content = changeForYaer;//让新增页面在弹出窗口显示
             
                }
           }
       
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }
        #endregion
    }
}
